package com.zb.goods.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.goods.service.ConGoodsService;
import com.zb.goods.service.ConProductKnowledgeTypeService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.*;
import com.zb.utils.TokenUtil;
import com.zb.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 * @author WEN
 * @since 2023-08-15
 */
@Api(tags = "商品成品类接口")
@RestController
@RequestMapping("/goods/product")
public class ZjProductController {
    @Resource
    private ConGoodsService conGoodsService;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjProductSpecService specService;
    @Resource
    private ZjProductIntroduceService introduceService;
    @Resource
    private ZjProductExtendPathService extendPathService;
    @Resource
    private ZjProductBoomService boomService;
    @Resource
    private ZjProductBoomChildService boomChildService;
    @Resource
    private ConProductKnowledgeTypeService conProductKnowledgeTypeService;
    @Resource
    private ZjKnowledgeTypeVideoService knowledgeTypeVideoService;
    @Resource
    private ZjKnowledgeTypeInstructionBookService knowledgeTypeInstructionBookService;
    @Resource
    private ZjKnowledgeTypeFaultService knowledgeTypeFaultService;
    @Resource
    private ViewGoodsService viewGoodsService;
    @Resource
    private ZjFactoryProductService factoryProductService;

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "商品资料-商品管理-产品 - 新增或修改产品")
    @ApiOperation("新增或修改产品")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid GoodsVo goodsVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conGoodsService.save(loginInfo, 1, goodsVo);
        return Result.success(null == goodsVo.getId() ? "新增产品成功" : "修改产品成功");
    }

    @ApiOperation("获取产品列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody GoodsSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取产品列表成功").data(productService.getList(custAccId, selectVo));
    }

    @ApiOperation("根据产品ID获取产品详情")
    @GetMapping("/getProductDetail/{id}")
    public Result getProductDetail(@PathVariable @ApiParam(value = "产品ID", required = true) Long id) {
        return Result.success("获取产品详情成功！").data("row", productService.getProductDetail(id));
    }

    @ApiOperation("根据产品ID获取产品修改前的信息")
    @GetMapping("/getInfo/{id}")
    public Result getInfo(@PathVariable @ApiParam(value = "产品ID", required = true) Long id) {
        return Result.success("获取产品信息成功").data("row", productService.getInfo(id));
    }

    @ApiOperation("根据产品ID获取产品知识库分类")
    @PostMapping("/getProductKnowledgeType/{id}")
    public Result getProductVideo(@PathVariable @ApiParam(value = "产品ID", required = true) Long id, @RequestBody PageUtil pageUtil) {
        return Result.success("获取产品知识库分类成功").data(conProductKnowledgeTypeService.getProductKnowledgeType(id, pageUtil));
    }

    @ApiOperation("根据知识仓库分类ID获取产品视频")
    @GetMapping("/getProductVideo/{knowledgeTypeId}")
    public Result getProductVideoDetail(@PathVariable @ApiParam(value = "知识仓库分类ID", required = true) Long knowledgeTypeId) {
        return Result.success("获取产品视频成功").data(PageUtil.row(knowledgeTypeVideoService.getVideo(knowledgeTypeId)));
    }

    @ApiOperation("根据知识仓库分类ID获取产品说明书")
    @GetMapping("/getProductInstructionBook/{knowledgeTypeId}")
    public Result getProductInstructionBook(@PathVariable @ApiParam(value = "知识仓库分类ID", required = true) Long knowledgeTypeId) {
        return Result.success("获取产品说明书成功").data(PageUtil.row(knowledgeTypeInstructionBookService.getInstructionBook(knowledgeTypeId)));
    }

    @ApiOperation("根据知识仓库分类ID获取产品故障库")
    @GetMapping("/getProductFault/{knowledgeTypeId}")
    public Result getProductFault(@PathVariable @ApiParam(value = "知识仓库分类ID", required = true) Long knowledgeTypeId) {
        return Result.success("获取产品故障库成功").data(PageUtil.row(knowledgeTypeFaultService.getProductFault(knowledgeTypeId)));
    }

    @ApiOperation("根据产品ID获取产品扩展连接")
    @GetMapping("/getProductExtendPath/{id}")
    public Result getProductExtendPath(@PathVariable @ApiParam(value = "产品ID", required = true) Long id) {
        return Result.success("获取产品扩展连接成功").data("row", extendPathService.getByProductId(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "商品资料-商品管理-产品 - 批量启用或禁用产品")
    @ApiOperation("批量启用或禁用产品")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "产品状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "商品ID集合", required = true) List<Long> idList) {
        productService.updateBatchDisabledByIds(idList, status);
        return Result.success(status ? "禁用产品成功" : "启用产品成功");
    }


    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.DELETE, remark = "商品资料-商品管理-产品 - 批量删除产品")
    @ApiOperation("批量删除产品")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "商品ID集合", required = true) List<Long> idList) {
        conGoodsService.deleteByIds(1, idList);
        return Result.success("删除产品成功");
    }

    @ApiOperation("根据产品ID获取产品参数")
    @GetMapping("/getProductParam/{id}")
    public Result getProductParam(@PathVariable @ApiParam(value = "产品ID", required = true) Long id) {
        return Result.success("获取产品参数").data("row", productService.getProductParam(id));
    }

    @ApiOperation("根据产品ID获取规格列表")
    @PostMapping("/getSpecList/{productId}")
    public Result getSpecList(@PathVariable @ApiParam(value = "产品ID", required = true) Long productId, @RequestBody GoodsDetailSpecSelectVo selectVo) {
        return Result.success("获取规格列表成功").data(specService.getSpecList(productId, selectVo));
    }

    @ApiOperation("根据产品ID获取产品简介")
    @GetMapping("/getIntroduce/{productId}")
    public Result getIntroduce(@PathVariable @ApiParam(value = "产品ID", required = true) Long productId) {
        return Result.success("获取产品介绍成功").data("row", introduceService.getByProductId(productId));
    }

    @ApiOperation("获取Boom关联的商品列表")
    @PostMapping("/getGoodsBoomList/{id}")
    public Result getGoodsBoomList(HttpServletRequest request, @PathVariable @ApiParam(value = "产品ID，获取Boom关联时，排除自身", required = true) Long id, @RequestBody @Valid GoodsBoomSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取Boom关联的商品成功").data(viewGoodsService.getGoodsBoomList(custAccId, 1, id, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "商品资料-商品管理-产品 - 新增或修改Boom关联的商品")
    @ApiOperation("新增或修改Boom关联的商品")
    @PostMapping("/saveGoodsBoom")
    public Result saveGoodsBoom(HttpServletRequest request, @RequestBody @Valid ProductBoomVo boomVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        boomService.save(custAccId, boomVo);
        return Result.success(null == boomVo.getId() ? "新增Boom关联商品成功" : "修改Boom关联商品成功");
    }

    @ApiOperation("根据产品Boom ID获取产品Boom详情")
    @GetMapping("/getProductBoom/{id}")
    public Result getProductBoom(@PathVariable @ApiParam(value = "产品Boom ID", required = true) Long id) {
        return Result.success("获取产品Boom详情成功").data("row", boomService.getInfoById(id));
    }

    @ApiOperation("根据产品Boom ID获取产品Boom子件列表")
    @PostMapping("/getProductBoomChildList/{productBoomId}")
    public Result getProductBoomChildList(@PathVariable @ApiParam(value = "产品Boom ID", required = true) Long productBoomId,
                                          @RequestBody PageUtil pageUtil) {
        return Result.success("获取产品Boom子件列表成功").data(boomChildService.getProductBoomChildList(productBoomId, pageUtil));
    }

    @ApiOperation("根据产品ID获取产品Boom清单")
    @GetMapping("/getBoom/{id}")
    public Result getBoom(@PathVariable @ApiParam(value = "产品ID", required = true) Long id) {
        return Result.success("获取产品Boom清单成功").data("rows", boomService.getByProductId(id));
    }

    @ApiOperation("获取工单报单产品")
    @PostMapping("/getOrderProduct")
    public Result getOrderProduct(HttpServletRequest request, @RequestBody OrderGoodsSelectVo goodsSelectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取工单报单产品成功").data(viewGoodsService.getOrderProduct(custAccId, goodsSelectVo));
    }

    @ApiOperation("获取出入库商品")
    @PostMapping("/getInOrOutStoreGoods/{type}")
    public Result getInOrOutStoreGoods(HttpServletRequest request, @PathVariable @ApiParam(value = "商品类型：1-成品，2-配件") Integer type,
                                       @RequestBody OrderGoodsSelectVo goodsSelectVo) {
        Assert.isTrue(Pattern.matches("[1|2]", type.toString()), "商品类型参数不合法！");
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取出入库商品成功").data(viewGoodsService.getInOrOutStoreGoods(custAccId, type, goodsSelectVo));
    }
    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "商品资料-商品管理-产品 - 批量保存关联品牌商家商品")
    @ApiOperation("批量保存关联品牌商家商品")
    @PostMapping("/saveFactoryProduct")
    public Result saveFactoryProduct(HttpServletRequest request, @RequestBody @Valid FactoryProductVo factoryProductVo)  {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        factoryProductService.save(custAccId, factoryProductVo);
        return Result.success("保存关联品牌商家商品成功");
    }


}
